#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File    : time_format.py
# @Description : 时间格式化

import datetime
import time

from utils.logger import log


class TimeFormat:
    """时间格式化"""
    @staticmethod
    def now_time(fmt: str = "%Y-%m-%d") -> str:
        """
        当前时间
        :param fmt: 时间格式
        :return: 返回当前时间
        """
        try:
            return datetime.datetime.now().strftime(fmt)
        except Exception as e:
            log.error(e)

    def now_month_range(fmt: str = "%Y-%m-%d") -> str:
        """
        当月首尾日期
        """
        # 获取当前日期
        today = datetime.date.today()

        # 获取当月开始日期
        start_date = today.replace(day=1)

        # 获取下个月开始日期
        next_month = start_date.replace(month=start_date.month + 1, day=1)

        # 获取当月结束日期
        end_date = next_month - datetime.timedelta(days=1)

        print("当月开始日期：", start_date)
        print("当月结束日期：", end_date)
        return start_date, end_date

    @staticmethod
    def before_n_days(n: int = -1, fmt: str = "%Y-%m-%d"):
        now = datetime.datetime.now()
        mouths_before = now + datetime.timedelta(days=n)
        return mouths_before.strftime(fmt)

    @staticmethod
    def n_days_later(n, fmt):
        """
        n天后的时间
        :param n: 天数
        :param fmt: 时间格式
        :return: 返回n天后的时间
        """
        try:
            fmt_time = (datetime.datetime.now() +
                        datetime.timedelta(days=n)).strftime(fmt)
            return fmt_time
        except Exception as e:
            log.error(e)

    @staticmethod
    def n_hours_later(n, fmt):
        """
        n小时后的时间
        :param n: 小时数
        :param fmt: 时间戳格式
        :return:
        """
        try:
            fmt_time = (datetime.datetime.now() +
                        datetime.timedelta(hours=n)).strftime(fmt)
            return fmt_time
        except Exception as e:
            log.error(e)

    @staticmethod
    def n_minutes_later(n, fmt):
        """
        n分钟后的时间
        :param n: 分钟
        :param fmt: 时间戳格式
        :return:
        """
        try:
            fmt_time = (datetime.datetime.now() +
                        datetime.timedelta(minutes=n)).strftime(fmt)
            return fmt_time
        except Exception as e:
            log.error(e)

    @staticmethod
    def n_seconds_later(n, fmt):
        """
        n秒后的时间
        :param n: 秒钟
        :param fmt: 时间戳格式
        :return:
        """
        try:
            fmt_time = (datetime.datetime.now() +
                        datetime.timedelta(seconds=n)).strftime(fmt)
            return fmt_time
        except Exception as e:
            log.error(e)

    @staticmethod
    def get_now_timestamp() -> int:
        """获取当前时间，并返回时间戳(13位毫秒级)
        :return: 当前时间戳
        :rtype: int
        """
        timestamp = int(datetime.datetime.now().timestamp() * 1000)
        return timestamp

    @staticmethod
    def minutes_later_timestamp(n):
        """
        n分钟后的时间戳
        :param n: 分钟
        :return:
        """
        try:
            timestamp = (datetime.datetime.now() +
                         datetime.timedelta(minutes=n))
            return timestamp
        except Exception as e:
            log.error(e)

    @staticmethod
    def timestamp_to_fmt_time(timestamp, fmt="%Y-%m-%d %H:%M:%S") -> str:
        """
        13位时间戳转化成时间格式
        :param timestamp: 时间戳
        :return:
        """
        timeStamp = float(timestamp / 1000)
        timeArray = time.localtime(timeStamp)
        otherStyleTime = time.strftime(fmt, timeArray)
        return otherStyleTime


time_fmt = TimeFormat()

if __name__ == "__main__":
    print(time_fmt.now_time(fmt="%Y/%m/%d"))
    # l = t.n_hours_later(n=100, fmt="%Y-%m-%d %H:%M:%S")
    # m = t.n_days_later(n=1, fmt="%Y-%m-%d")
    # n = t.now_time(fmt="%Y-%m-%d")
    # print(type(n))
